<?php 
namespace common\service;

use common\model\sell66\Bot;
use common\model\sell66\UserOrder;
use common\model\sell66\UserSteam;
use think\Config;
use think\Cache;

/**
 * 用户订单
 * @author zhouqiang
 * @date 2017/07/07
 */
class UserOrderService
{
	
	public static $instance;
	
	public static function getInstance()
	{
		if (self::$instance == null) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	public function getRandTitle()
	{
		return Config::get('other_ranking')[rand(0,4)];
	}

	function substr_cut($chars)
	{
		$strlen = mb_strlen($chars, 'utf-8');
 
		if ( $strlen<2 ) {
		    return $chars;
		} else {
		    $firstStr = mb_substr($chars, 0, 1, 'utf-8');
		    $lastStr = mb_substr($chars, -1, 1, 'utf-8');
		    return $strlen == 2 ? $firstStr . str_repeat('*', mb_strlen($chars, 'utf-8') - 1) : $firstStr . str_repeat("*", $strlen - 2) . $lastStr;
		}
	}

	function diffDays($day1, $day2)
	{
		$second1 = strtotime($day1);
		$second2 = strtotime($day2);

		if ($second1 < $second2) {
			$tmp = $second2;
			$second2 = $second1;
			$second1 = $tmp;
		}
		return ($second1 - $second2) / 86400;
	}

	public function getTopOrderDetails()
	{
		$topRanking = array(500,200,100);

		$startTime = strtotime('2017-07-11');
		$lastTime = strtotime('2017-08-01');

		$details = Cache::store('redis')->get('top_order_details');
		if (empty($details)) {
			$details = UserOrder::getTopOrderDetails($startTime, $lastTime);
			if ($details) {
				Cache::store('redis')->set('top_order_details', $details, 300);
			}
		}
		
		if (empty($details)) {
			return array();
		}

		foreach ($details as $rows => &$ret) {
			$ret['rank'] = $rows + 1;
			if (isset($topRanking[$rows])) {
				$ret['title'] = $topRanking[$rows];
			}

			$ret['personaname'] = $this->substr_cut($ret['personaname']);
			unset($ret['total_price']);
		}
		return $details;
	}

	public function getUserTopOrderRanking($userId)
	{
		$data = array();
		$topRanking = Config::get('top_ranking');
		$startTime = strtotime('2017-07-11');
		$lastTime = strtotime('2017-08-01');

		if (!$userId) {
			return array(
				'rank' => '---',
				'num' => '---',
				'user_id' => '---',
				'personaname' => '---',
				'avatarmedium' => 'https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/fe/fef49e7fa7e1997310d705b2a6158ff8dc1cdfeb_medium.jpg',
				'title' => $this->getRandTitle(),
			);
		}

		$count = UserOrder::getUserOrderTotalNum($userId, $startTime, $lastTime);
		if (!$count) {
			$userDetails = UserSteam::getUserInfo($userId);
			$data = array(
				'rank' => '暂无',
				'num' => 0,
				'user_id' => $userId,
				'personaname' => $userDetails->personaname,
				'avatarmedium' => $userDetails->avatarmedium,
				'title' => $this->getRandTitle(),
			);
			return $data;
		}

		$details = Cache::store('redis')->get('order_ranking');
		if (empty($details)) {
			$details = UserOrder::getUserTopOrderRanking($startTime, $lastTime);
			Cache::store('redis')->set('order_ranking', $details, 300);
		}

		foreach ($details as $rows => $ret) {

			if ($ret['user_id'] == $userId) {
				$data['rank'] = $rows + 1;
				$data['num'] = $ret['num'];
				$data['user_id'] = $ret['user_id'];
				$data['personaname'] = $ret['personaname'];
				$data['avatarmedium'] = $ret['avatarmedium'];
				if (isset($topRanking[$rows])) {
					$data['title'] = $topRanking[$rows];
				}

				if ($rows > 2) {
					$data['title'] = $this->getRandTitle();
				}
				break;
			}
		}
		return $data;
	}
}